/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package themsanpham;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.String;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author ToanNhan
 */
public class ChiTietSanPhamClass {
    private String tenNhomSanPham;
    private ArrayList<NhomThongTinSanPhamClass> nhomThongTinSanPhams;

    /**
     * @return the nhomThongTinSanPhams
     */
    public ArrayList<NhomThongTinSanPhamClass> getNhomThongTinSanPhams() {
        return nhomThongTinSanPhams;
    }

    /**
     * @param nhomThongTinSanPhams the nhomThongTinSanPhams to set
     */
    public void setNhomThongTinSanPhams(ArrayList<NhomThongTinSanPhamClass> nhomThongTinSanPhams) {
        this.nhomThongTinSanPhams = nhomThongTinSanPhams;
    }

    public String toXml() throws ParserConfigurationException, TransformerConfigurationException, TransformerException, UnsupportedEncodingException, FileNotFoundException, IOException{
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        Element rootElement = document.createElement(tenNhomSanPham);
        document.appendChild(rootElement);
        for (NhomThongTinSanPhamClass nt : nhomThongTinSanPhams){
            Element el = document.createElement("NhomThongTinSanPham");            
            el.setAttribute("name", nt.getTenNhomThongTinSanPham());
            for (ThongTinSanPhamClass tt : nt.getThongTinSanPhams()){
                Element child = document.createElement("ThongTinSanPham");
                child.setAttribute("name", tt.getTenThonTin());
                child.setAttribute("value", tt.getGiaTri());
                el.appendChild(child);
            }
            rootElement.appendChild(el);
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(buffer);
        StreamResult result = new StreamResult(dos);
        transformer.transform(source, result);
        
        String str = buffer.toString("UTF8");        
        return str;

    }

    /**
     * @return the tenNhomSanPham
     */
    public String getTenNhomSanPham() {
        return tenNhomSanPham;
    }

    /**
     * @param tenNhomSanPham the tenNhomSanPham to set
     */
    public void setTenNhomSanPham(String tenNhomSanPham) {
        this.tenNhomSanPham = tenNhomSanPham;
    }

    public ChiTietSanPhamClass(){
        tenNhomSanPham = "";
        nhomThongTinSanPhams = new ArrayList<NhomThongTinSanPhamClass>();
    }

}
